/**
 * Created by niefengjun on 14-12-2.
 */
var db = require('./mysql/texts');
var tool = require('./tool');

exports.index = function (req, res) {
    res.render('index.ejs');

}
function indexs(req, res) {
    var AjaxType = req.query.AjaxType;
    console.log(AjaxType) ;
    switch (AjaxType) {

        case "list"://
            list(req, res);
            break;


        default :
            res.render('selectse.ejs', { title: 'Express' });
            break;
    }
}


function list(req, res) {

    db.list(function (err, vaue) {
            if (!err) {
                res.send(vaue);
            }
            else {
                res.send('-1');
            }
        }
    )
}
exports.indexs = indexs;

exports.add = function(req,res){
    var username = req.query.p;
    var password = req.query.u;
    if(username != null){
        db.add(username,password,function(err,result){
            console.log(err,result) ;
            if(!err){
              if(result&&result.length>0){
                    db.add(username,password,function(err,value){});
                    res.send("0");
                }else {
                    db.add(username,password,function(err,value){});
                    res.send("0");
                }
            }else{
                res.send("0");
            }

        });
    }
}
exports.checkLogin = function (req, res, next) {
    var url = req.originalUrl;
    // console.log(url) ;
    if (!(url.indexOf('/style') > -1 || url.indexOf('/bootstrap') > -1 || url.indexOf('/fancybox') > -1 || url.indexOf('/fonts') > -1 || url.indexOf('/images') > -1 || url.indexOf('/js') > -1 || url.indexOf('/less') > -1 || url.indexOf('/scss') > -1)) {
        if (req.cookies["renrenxiangct"]) {

            next();

        } else {
            res.redirect("/");
        }


    } else {

        next();
    }
}



